﻿using Liju.Common;
using System;
using System.Windows.Forms;

namespace LiJu
{
    public partial class SecondScan : Form
    {
        /// <summary>
        /// 二次扫码
        /// </summary>
        public string ScanSN2;

        /// <summary>
        /// 扫码状态 true已扫码 false 待扫码
        /// </summary>
        public bool State = false;

        public SecondScan()
        {
            InitializeComponent();
        }

        private void SecondScan_Load(object sender, System.EventArgs e)
        {
            ScanSN2 = tbSN.Text = "";
            UnifiedObj.WriteLog(1, "二次扫码弹框 开始\r\n");

        }

        private void tbSN_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)//回车触发
            {
                if (tbSN.Text.Trim().Length < 1)
                {
                    return;
                }
                UnifiedObj.WriteLog(1, "二次扫码原始值："+ tbSN.Text);

                try
                {
                    //判断条码是否误识别2次；变成36位
                    if (tbSN.Text.Trim().Length > 12 && tbSN.Text.Trim().Length % 2 == 0)
                    {
                        if (tbSN.Text.Trim().Replace(tbSN.Text.Trim().Substring(0, tbSN.Text.Trim().Length / 2), "").Length == 0)
                        {
                            tbSN.Text = tbSN.Text.Trim().Substring(0, tbSN.Text.Trim().Length / 2);
                            this.Invoke(new MethodInvoker(() =>
                            {
                                //光标移到最后
                                tbSN.SelectionStart = tbSN.Text.Length;
                                tbSN.SelectionLength = 0;
                            }));
                        }
                    }
                    ScanSN2 = tbSN.Text.Trim();
                    State = true;
                    this.Close();
                }
                catch (Exception ex)
                {
                    UnifiedObj.WriteLog(1, "二次扫码窗体异常：" + ex.Message);
                }
            }
        }
    }
}
